package day_2022_9_to_12.Day;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author haomin
 * @date 2022/09/07 20:21
 **/
public class Day5_7_Num870_AdvantageCount {
    class Solution {
        public int[] advantageCount(int[] nums1, int[] nums2) {
            Arrays.sort(nums1);
            PriorityQueue<int[]> pq = new PriorityQueue<>((int[] a, int[] b) -> b[1]-a[1]);
            for(int i = 0; i < nums2.length; ++i) pq.offer(new int[]{i, nums2[i]});
            int[] res = new int[nums2.length];
            int l = 0, r = nums1.length-1;
            while(!pq.isEmpty()){
                int[] ret = pq.poll();
                int i = ret[0], maxVal = ret[1];
                if(nums1[r] > maxVal){
                    res[i] = nums1[r];
                    r--;
                }else {
                    res[i] = nums1[l];
                    l++;
                }
            }
            return res;
        }
    }
}